#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# -= project0/entry.py coding by Scott in 2019-06-05 16:33 with IDE:PyCharm =-
from flask import Flask, redirect, request
from xml.dom.minidom import parseString

from config import Cfg
from utils import verify
from utils.msg_handler import Handler

app = Flask(__name__)


class Config(object):
    """工程配置信息"""
    DEBUG = False


app.config.from_object(Config)


@app.route('/wx/', methods=['GET', 'POST'])
def index():
    if verify(request.args):
        if request.method == 'GET':
            return request.args.get('echostr')
        msg = request.data.decode()
        dom = parseString(msg)
        return post_handler(dom)
    else:
        return redirect('https://www.baidu.com')


def post_handler(dom):
    appid = Cfg.appid
    msg_type = dom.getElementsByTagName('MsgType')[0].childNodes[0].data
    handler = Handler(appid, msg_type, dom)
    return handler.run()


if __name__ == '__main__':
    app.run()
